package com.hengtiansoft.lfy.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ListView;
import android.widget.TextView;

import com.hengtiansoft.lfy.R;

/**
 * Created by daiye on 2016/10/24 0024.
 */

public class LoadMoreListView extends ListView implements AbsListView.OnScrollListener {
    View footer;
    int totalItemCount;
    int lastVisibleItem;
    boolean isLoading;
    ILoadListener iLoadListener;
    private onScrollListener mListener = null;
    public LoadMoreListView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
        initView(context);
    }

    public LoadMoreListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
        initView(context);
    }

    public LoadMoreListView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        // TODO Auto-generated constructor stub
        initView(context);
    }

    /**
     * listview
     *
     * @param context
     */
    private void initView(Context context) {
        LayoutInflater inflater = LayoutInflater.from(context);
        footer = inflater.inflate(R.layout.footer_layout, null);
        footer.findViewById(R.id.load_layout).setVisibility(View.GONE);
        this.addFooterView(footer);
        this.setOnScrollListener(this);
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem,
                         int visibleItemCount, int totalItemCount) {
        // TODO Auto-generated method stub
        this.lastVisibleItem = firstVisibleItem + visibleItemCount;
        this.totalItemCount = totalItemCount;
        if(mListener!=null)
            mListener.onScroll(view,firstVisibleItem,visibleItemCount,totalItemCount);
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        // TODO Auto-generated method stub
        if (totalItemCount == lastVisibleItem
                && scrollState == SCROLL_STATE_IDLE) {
            if (!isLoading) {
                isLoading = true;
                footer.findViewById(R.id.load_layout).setVisibility(View.VISIBLE);
                footer.findViewById(R.id.no_comment_tv).setVisibility(View.GONE);
                // ���ظ���
                iLoadListener.onLoad();
            }
        }
        if(mListener!=null)
            mListener.onScrollStateChanged(view,scrollState);
    }
    /**
     * �������
     */
    public void loadComplete(){
        isLoading = false;
        footer.findViewById(R.id.load_layout).setVisibility(
                View.GONE);
    }

    public void setInterface(ILoadListener iLoadListener){
        this.iLoadListener = iLoadListener;
    }

    public interface ILoadListener{
        public void onLoad();
    }

    public void setNoCommentViewVision(boolean isVision,String text){
        if(isVision){
            loadComplete();
            footer.findViewById(R.id.no_comment_tv).setVisibility(View.VISIBLE);
            ((TextView)footer.findViewById(R.id.no_comment_tv)).setText(text);
        }else{
            footer.findViewById(R.id.no_comment_tv).setVisibility(View.GONE);
        }
    }

    public void setScrollListener(onScrollListener l){
        mListener = l;
    }

    public interface onScrollListener {
        public void onScrollStateChanged(AbsListView view, int scrollState);
        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount);
    }

}